package com.fnzn.entranceGuard.api;

import com.fnzn.entranceGuard.util.AjaxJson;
import com.fnzn.entranceGuard.util.DateTools;
import com.fnzn.entranceGuard.util.QRGenerate;
import com.fnzn.entranceGuard.util.qrCode.QRCodeEncoder;
import com.github.pig.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;


/**
 * @author wf_sxf@163.com
 * @date 2019-01-03 9:08
 */
@Controller
@Slf4j
@RequestMapping("/api/finger/")
public class FingerApi extends BaseController {


    /**
     * 獲取指纹设置二维码
     * @param companyId 当前公司ID
     * @return
     */
    @GetMapping("getFingerQr")
    @ResponseBody
        public AjaxJson getFingerQr(Integer companyId) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        try {
            //TODO 指纹协议
            String content = "111";
            QRCodeEncoder encoder = new QRCodeEncoder();
            BufferedImage image = encoder.encoderQRCode(QRGenerate.generateQr(content), "", "png", "UTF-8", 4, null, null, null, 6, null, null);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(image, "png", os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            BASE64Encoder baseEncoder = new BASE64Encoder();
            json.setObj(baseEncoder.encode(os.toByteArray()));
        } catch (Exception e) {
            json.setSuccess(false);
            json.setMsg("获取指纹二维码异常，" + e.getMessage());
            log.error("获取指纹二维码异常，", e.getMessage());
        }
        return json;
    }
    public static void main(String[] args) {
        try {
            int days = DateTools.getCurrentMonthLastDay("20181102");
            System.out.println(days);

        } catch (Exception e) {

        }
    }


}
